<?php
namespace app\material\model;
use app\BaseModel;
use app\shop\model\AdminCompany;
use think\facade\Db;



class BrochureCompany extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_material_brochure_company';


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * User: chenniang
     * Date: 2019-09-25 15:29
     * @return void
     * descption:转换插入数据
     */
    public function changeData($ids){
        $data = [];
        if(is_array($ids)){

            foreach ($ids as $k =>$v){
                $data[] = array_pop($v);
            }
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->where('status','>',-1)->order('id desc')->paginate($page)->toArray();


        return $data;

    }


    /**
     * @param $dis
     * @return array
     * 获取company
     */

    public function getId($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_company b','a.company_id=b.id')
            ->where($dis)
            ->column('company_id');
        $company_id = [];
        if(!empty($data)){
            foreach ($data as $v){
                $company_id[] = $this->reset($v);
            }
        }
        return array_values($company_id);
    }



    /***
     * @param $dis
     * @功能说明:公共商品获取公司id
     * @author lxj
     * @DataTime: 2020/5/14 14:06
     */
    public function getCompanyIds($dis){
        $data = AdminCompany::where($dis)->column('id');
        $company_id = [];
        if(!empty($data)){
            foreach ($data as $v){
                $company_id[] = $this->reset($v);
            }
        }
        return array_values($company_id);
    }


    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){
        $cate   = Db::name('longbing_card_company')->where(['id'=>$id])->find();
        if(!empty($cate)&&$cate['pid']!=$cate['id']){
            array_unshift($data,$cate['id']);
            $this->reset($cate['pid'],$data);
        }
        return $data;
    }




}